package com.my.hzb.user.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.framelib.utils.Page;
import com.my.hzb.entiy.UserInfo;
import com.my.hzb.entiy.UserInfoExample;
import com.my.hzb.entiy.UserInfoExample.Criteria;
import com.my.hzb.mapper.UserInfoMapper;
import com.my.hzb.order.service.OrderService;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserInfoMapper userInfoMapper;
//	@Autowired
//	private OrderService orderService;
	
	@Override
	public void add(UserInfo user) {
		user.setCreateTime(new Date());
		user.setUpdateTime(new Date());
		System.out.println("user add ="+userInfoMapper.insert(user));
	}

	@Override
	public void update(UserInfo user) {
	}

	@Override
	public void delete(UserInfo user) {
		userInfoMapper.deleteByPrimaryKey(user.getId());
	}

	@Override
	public List<UserInfo> find(UserInfo user,Page page) {
		UserInfoExample example = new UserInfoExample();
		Criteria criteria = example.createCriteria();
		if(null!=user.getUserName()){
			criteria.andUserNameEqualTo(user.getUserName());
		}else if(null!=user.getUserPwd()){
			criteria.andUserPwdEqualTo(user.getUserPwd());
		}else if(null!=user.getStatus()){
			criteria.andStatusEqualTo(user.getStatus());
		}
		if(null!=page){
			example.setPage(page);
		}
		return userInfoMapper.selectByExample(example);
	}

	@Override
	public UserInfo getOne(Integer id) {
		return userInfoMapper.selectByPrimaryKey(id);
	}
	@Override
	public UserInfo getUserByName(String userName) {
		UserInfoExample userInfoExample = new UserInfoExample();
		userInfoExample.createCriteria().andUserNameEqualTo(userName);
		List<UserInfo> userList = userInfoMapper.selectByExample(userInfoExample);
		if(null!=userList&&userList.size()>0){
			return userList.get(0);
		}
		return null;
	}

	@Override
	public int getCount(UserInfo user) {
		UserInfoExample example = new UserInfoExample();
		Criteria criteria = example.createCriteria();
		if(null!=user.getUserName()){
			criteria.andUserNameEqualTo(user.getUserName());
		}else if(null!=user.getUserPwd()){
			criteria.andUserPwdEqualTo(user.getUserPwd());
		}else if(null!=user.getStatus()){
			criteria.andStatusEqualTo(user.getStatus());
		}
		return userInfoMapper.countByExample(example);
	}

	@Override
	public List<UserInfo> findByPage(UserInfo user, Page page) {
		UserInfoExample example = new UserInfoExample();
		example.setPage(page);
		return userInfoMapper.selectByExample(example);
	}

}
